* Table: Post-match community-level summary statistics and balance

use "${data}uttarakhand_census_2001.dta", clear

// Indicate NGO's portfolio of villages in these districts
destring v_ct_code, replace
merge 1:1 v_ct_code using "${data}uttarakhand_surveyed_villages.dta"
replace teri = 0 if teri == . // Prior/current intervention by TERI NGO
replace chirag = 0 if chirag == . // Prior/current intervention by CHIRAG NGO

// Add in the codes for the three mis-classified villages (previously linked to Almora)
replace chirag = 1 if v_ct_code == 01127300 | v_ct_code == 01131700 | v_ct_code == 01070800

// Remove TERI-only villages
drop if teri == 1 & chirag == 0

// Remove villages without indicated population
drop if (t_p == 0 | t_p == .)
gen small = (t_hh < 40) // fewer than 40 households in the village

// Generate block-level dummies
tab block_code if chirag == 1 | teri == 1 // only blocks 67 68 73 74 75 76 78 and 79 have intervention villages
destring block_code, replace
for any 67 68 73 74 75 76 78 79: gen blockX = 1 if block_code == X
for any 67 68 73 74 75 76 78 79: replace blockX = 0 if blockX == .

// Generate density statistics
gen sc_pct = sc_p/t_p
gen st_pct = st_p/t_p
gen density = t_p/area

********************************************************************************

* Match with sample intervention villages
ren v_ct_code plcn2001
preserve
	use "${data}uttarakhand_census_2001_2011_chirag", clear
	keep if census == 2001
	keep plcn2001 chirag_village
	tempfile temp1    /* create a temporary file */
	save `temp1'      /* save memory into the temporary file */
restore 
merge 1:1 plcn2001 using "`temp1'", gen(_merge_chirag)
keep if !mi(chirag_village)
sort chirag_village plcn2001

********************************************************************************

// Designate post-match balance variables
global communityvar area t_p sc_pct st_pct p_sch m_sch h_cntr ph_cntr phone bs_fac crsoc_fac app_pr dist_town land_fores tap power_all   

tab dist_code, gen(dist_dummy)

// Balance table
iebaltab $communityvar, grpvar(chirag_village) normdiff stdev ///
	rowlabels("area Area (km\textsuperscript{2}) @ t_p Total population @ sc_pct Scheduled Caste population share @ st_pct Scheduled Tribe population share @ p_sch Number of primary schools @ m_sch Number of middle schools @ h_cntr Number of health centers @ ph_cntr Number of primary health centers @ phone Number of telephone connections @ bs_fac \(\mathbbm{1}\left( \text{Bus services} \right)\) @ crsoc_fac \(\mathbbm{1}\left( \text{Credit societies} \right)\) @ app_pr \(\mathbbm{1}\left( \text{Approach to village: paved road} \right)\) @ dist_town Distance from nearest town (km) @ land_fores Forest area (hectares) @ tap \(\mathbbm{1}\left( \text{Tap water} \right)\) @ power_all \(\mathbbm{1}\left( \text{Electricity for all purposes} \right)\)") /// 
	order(1 0) format(%9.2f) tblnonote ///
	grplabels("1 NGO villages @ 0 Non-NGO villages") ///
	fixedeffect(dist_code) ///
	browse onerow

// Replace with minus signs for LaTeX
foreach var of varlist v4 v5 {
	replace `var' = subinstr(`var', "-", "\(-\)",.) if _n >= 3
}

// Save balance table
save "${output}table_postmatch_balance.dta", replace

// Export table to LaTeX
// drop v4 // no statistically significant differences - dropping t-test column
drop if _n < 4 // drop table headers
drop if v1 == "N" // drop observations row
texsave using "${results}table_postmatch_balance.tex", ///
	frag replace nonames nofix
